SRC_DIR = src/c++/$(PROJECT_NAME)
PROJECT_NAMES = graph_updater server test/astar test/tiny test/raster
SRCS = $(wildcard $(SRC_DIR)/*.cpp)
LIBS = boost_date_time boost_system pthread boost_filesystem boost_thread expat boost_unit_test_framework gmp

#CFLAGS = -Wall -pedantic -ggdb
PROCESSOR_COUNT = 2

#first part, which executes second by recursive make call
first: server-release
$(patsubst %,%-debug,$(PROJECT_NAMES)): %-debug:
	@make makedirs DIR=build/debug/$* PROJECT_NAME=$* -s 
	@make build/debug/$*/$* \
		  DIR=build/debug \
		  FLAGS='-g -O0 -fno-inline -D_DEBUG' \
		  PROJECT_NAME=$* -j$(PROCESSOR_COUNT)

$(patsubst %,%-debug-O2,$(PROJECT_NAMES)): %-debug-O2:
	@make makedirs DIR=build/debug-O2/$* PROJECT_NAME=$* -s 
	@make build/debug-O2/$*/$* \
		  DIR=build/debug-O2 \
		  FLAGS='-g -O2 -fno-inline -D_DEBUG' \
		  PROJECT_NAME=$* -j$(PROCESSOR_COUNT)
	  
$(patsubst %,%-release,$(PROJECT_NAMES)): %-release:
	@make makedirs DIR=build/release/$* PROJECT_NAME=$* -s
	@make build/release/$*/$* \
		  DIR=build/release \
    	  FLAGS='-DNDEBUG -O2' \
		  PROJECT_NAME=$* -j$(PROCESSOR_COUNT)

#second part
.PHONY: makedirs clean
makedirs:
	@echo "building " $(PROJECT_NAME)
	@mkdir -p $(dir $(subst $(SRC_DIR),$(DIR),$(SRCS))) $(dir $(subst $(SRC_DIR),.depend,$(SRCS)))
clean:
	-@rm -rf build/release/$(PROJECT_NAME) build/debug/$(PROJECT_NAME) build/debug-O2/$(PROJECT_NAME)  .depend/*

-include $(patsubst $(SRC_DIR)/%.cpp,.depend/%.dep,$(SRCS))

$(DIR)/$(PROJECT_NAME)/$(PROJECT_NAME): $(patsubst $(SRC_DIR)/%.cpp,$(DIR)/$(PROJECT_NAME)/%.o,$(SRCS))
	g++ $^ -o $@ $(patsubst %,-l%,$(LIBS))
$(DIR)/$(PROJECT_NAME)/%.o: $(SRC_DIR)/%.cpp
	g++ $< -c $(CFLAGS) -o $@ $(FLAGS) -MMD -MT $@ -MF .depend/$*.dep -I ./src/c++/include
